package com.itzxx.huazhun.data.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query


/**
 * Desc:
 *
 * Author: zhangxiaoxiao
 * Date: 2024/8/6 19:00
 * Copyright: Copyright (c) 2006-2024
 * Company: @海晟融创
 * Updater:
 * Update Time:
 * Update Comments:
 */

@Dao
interface ProbeRecordDao {
    //添加  传递一个参数 对象
    @Insert
    fun insert(item: ProbeRecordEntity)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAll(items: List<ProbeRecordEntity>)

    @Query("SELECT * FROM probe_record WHERE mac = :probeMac AND recordStartTime = :startTime ORDER BY id ASC")
    fun getProbeRecordList(probeMac: String, startTime: String): List<ProbeRecordEntity>

    @Query("SELECT MAX(temperature) FROM probe_record WHERE mac = :probeMac AND recordStartTime = :startTime")
    fun getProbeRecordListTempMax(probeMac: String, startTime: String): Double

    @Query("SELECT MIN(temperature) FROM probe_record WHERE mac = :probeMac AND recordStartTime = :startTime")
    fun getProbeRecordListTempMin(probeMac: String, startTime: String): Double

    @Query("SELECT AVG(temperature) FROM probe_record WHERE mac = :probeMac AND recordStartTime = :startTime")
    fun getProbeRecordListTempAvg(probeMac: String, startTime: String): Double

    @Query("SELECT * FROM probe_record WHERE mac = :probeMac AND recordStartTime = :startTime AND temperature >= :value ORDER BY id ASC LIMIT 1 OFFSET 0")
    fun getProbeRecordTempFirstGreaterThan(
        probeMac: String,
        startTime: String,
        value: Double
    ): ProbeRecordEntity

    @Query("SELECT MAX(airPressure) FROM probe_record WHERE mac = :probeMac AND recordStartTime = :startTime")
    fun getProbeRecordListAirPressureMax(probeMac: String, startTime: String): Double

    @Query("SELECT MIN(airPressure) FROM probe_record WHERE mac = :probeMac AND recordStartTime = :startTime")
    fun getProbeRecordListAirPressureMin(probeMac: String, startTime: String): Double

    @Query("SELECT * FROM probe_record WHERE mac = :probeMac AND recordStartTime = :startTime And airPressure = (SELECT MIN(airPressure) FROM probe_record WHERE mac = :probeMac AND recordStartTime = :startTime) ")
    fun getProbeRecordListAirPressureMinData(probeMac: String, startTime: String): ProbeRecordEntity


    @Query("SELECT AVG(airPressure) FROM probe_record WHERE mac = :probeMac AND recordStartTime = :startTime")
    fun getProbeRecordListAirPressureAvg(probeMac: String, startTime: String): Double

    @Query("SELECT * FROM probe_record WHERE mac = :probeMac AND recordStartTime = :startTime And id>:id LIMIT :limit")
    fun getProbeRecordListLimit(
        probeMac: String,
        startTime: String,
        id: Long,
        limit: Int
    ): List<ProbeRecordEntity>

    @Query("DELETE FROM probe_record WHERE mac = :probeMac AND recordStartTime = :startTime")
    fun delectProbeRecordList(probeMac: String, startTime: String)
}